package com.dmooo.yxmmerchant.view.presenter;

import android.content.Context;

import com.common.mvpbase.BasePresenter;
import com.common.net.listener.HttpOnNextListener;
import com.common.net.util.JSONUtils;
import com.common.util.LogUtil;
import com.dmooo.yxmmerchant.domain.MerchantAlbBean;
import com.dmooo.yxmmerchant.view.contract.MerchantAlbContract;
import com.dmooo.yxmmerchant.view.model.MerchantAlbModel;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.List;

public class MerchantAlbPresenter extends BasePresenter<MerchantAlbContract.MerchantAlbView> implements MerchantAlbContract.MerchantAlbPtr {
    private MerchantAlbModel model;
    GsonBuilder builder = new GsonBuilder();
    public MerchantAlbPresenter(MerchantAlbContract.MerchantAlbView mView, Context context) {
        super(mView);
        model=new MerchantAlbModel(context);
        builder.serializeNulls();
    }

    @Override
    public void getImgList(String token) {
        model.getImgList(token, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                LogUtil.d("sdafsd", o.toString());
                try {
                    mView.showImgList(new Gson().fromJson(new JSONObject(builder.create().toJson(o)).toString(), MerchantAlbBean.class).list);
                } catch (JSONException e) {
                    e.printStackTrace();
                    mView.errorMsg("数据解析错误");
                }
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void addImg(String token, List<File> img) {
        model.addImg(token, img, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                mView.addSuccess();
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void delImg(String token, String id, final int pos) {
        model.delImg(token, id, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                mView.delSuccess(pos);
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getNotice(String token) {
        model.getNotice(token, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                if(o.toString().contains("null")){
                    mView.showNotice("");
                    return;
                }
                JsonObject obj=JSONUtils.getAsJsonObject(o);
                mView.showNotice(obj.get("notice").getAsString());
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void editNotice(String token, String content) {
        model.editNotice(token, content, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                mView.editNoticeSuccess();
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}
